setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); return $pdo; } catch(PDOException $e) { return null; } } function initDatabase() { $pdo = getDBConnection(); if (!$pdo) return false; try { $tables = [ "CREATE TABLE IF NOT EXISTS clients ( id INT PRIMARY KEY AUTO_INCREMENT, nom VARCHAR(100) NOT NULL, prenom VARCHAR(100), email VARCHAR(100) NOT NULL, telephone VARCHAR(20), adresse VARCHAR(255) NOT NULL, code_postal VARCHAR(10), ville VARCHAR(100), date_creation DATETIME DEFAULT CURRENT_TIMESTAMP )", "CREATE TABLE IF NOT EXISTS techniciens ( id INT PRIMARY KEY AUTO_INCREMENT, nom VARCHAR(100) NOT NULL, prenom VARCHAR(100), email VARCHAR(100) NOT NULL, telephone VARCHAR(20), avatar VARCHAR(255), date_embauche DATE, actif BOOLEAN DEFAULT TRUE )", "CREATE TABLE IF NOT EXISTS interventions ( id INT PRIMARY KEY AUTO_INCREMENT, client_id INT NOT NULL, technicien_id INT, service VARCHAR(100) NOT NULL, description TEXT, statut ENUM('planifié', 'en_cours', 'terminé', 'annulé') DEFAULT 'planifié', progression INT DEFAULT 0, date_debut DATETIME, date_fin DATETIME, date_planification DATETIME DEFAULT CURRENT_TIMESTAMP, email_debut_envoye BOOLEAN DEFAULT FALSE, email_fin_envoye BOOLEAN DEFAULT FALSE, urgence BOOLEAN DEFAULT FALSE, adresse_intervention VARCHAR(255), notes TEXT, FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE CASCADE, FOREIGN KEY (technicien_id) REFERENCES techniciens(id) ON DELETE SET NULL )", "CREATE TABLE IF NOT EXISTS email_logs ( id INT PRIMARY KEY AUTO_INCREMENT, intervention_id INT, type ENUM('debut', 'fin', 'rappel') NOT NULL, destinataire VARCHAR(100) NOT NULL, sujet VARCHAR(255) NOT NULL, message TEXT, date_envoi DATETIME DEFAULT CURRENT_TIMESTAMP, statut ENUM('envoye', 'echec') DEFAULT 'envoye', FOREIGN KEY (intervention_id) REFERENCES interventions(id) ON DELETE CASCADE )" ]; foreach ($tables as $sql) { $pdo->exec($sql); } // Données de démonstration $stmt = $pdo->query("SELECT COUNT(*) FROM techniciens"); if ($stmt->fetchColumn() == 0) { $pdo->exec("INSERT INTO techniciens (nom, prenom, email, telephone, avatar) VALUES ('Rugovac', 'Senhia', 'senhia.rugovac@homeplus.lu', '+352 621 12 34 56', 'https://i.pravatar.cc/100?img=1'), ('Palocevic', 'Gordana', 'gordana.palocevic@homeplus.lu', '+352 621 29 54 94', 'https://i.pravatar.cc/100?img=2'), ('Sergio', 'Aires', 'sergio.aires@homeplus.lu', '+352 621 24 58 96', 'https://i.pravatar.cc/100?img=3')"); } $stmt = $pdo->query("SELECT COUNT(*) FROM clients"); if ($stmt->fetchColumn() == 0) { $pdo->exec("INSERT INTO clients (nom, prenom, email, telephone, adresse, code_postal, ville) VALUES ('Dupont', 'Jean', 'jean.dupont@email.lu', '+352 661 12 34 56', '10, rue de la Terre Noire', '4842', 'Rodange'), ('Lefèvre', 'Marie', 'marie.lefevre@email.lu', '+352 661 23 45 67', '5, avenue de la Gare', '1234', 'Luxembourg')"); } return true; } catch(PDOException $e) { return false; } } // ============ API ============ $action = isset($_GET['action']) ? $_GET['action'] : ''; $method = $_SERVER['REQUEST_METHOD']; if ($action) { header('Content-Type: application/json'); header('Access-Control-Allow-Origin: *'); $db = getDBConnection(); if (!$db) { echo json_encode(['success' => false, 'error' => 'Erreur de connexion à la base de données']); exit; } try { switch ($action) { case 'get_interventions': $sql = "SELECT i.*, c.nom as client_nom, c.prenom as client_prenom, c.email as client_email, c.adresse as client_adresse, t.nom as technicien_nom, t.prenom as technicien_prenom, t.email as technicien_email, t.avatar FROM interventions i JOIN clients c ON i.client_id = c.id LEFT JOIN techniciens t ON i.technicien_id = t.id ORDER BY i.date_planification DESC"; $stmt = $db->query($sql); $interventions = $stmt->fetchAll(); foreach ($interventions as &$i) { $i['technicien'] = [ 'nom' => $i['technicien_nom'] ?? '', 'prenom' => $i['technicien_prenom'] ?? '', 'email' => $i['technicien_email'] ?? '', 'avatar' => $i['avatar'] ?? 'https://i.pravatar.cc/100?img=1' ]; $i['client'] = [ 'nom' => $i['client_nom'], 'prenom' => $i['client_prenom'], 'email' => $i['client_email'], 'adresse' => $i['client_adresse'] ]; unset($i['client_nom'], $i['client_prenom'], $i['client_email'], $i['client_adresse']); unset($i['technicien_nom'], $i['technicien_prenom'], $i['technicien_email'], $i['avatar']); } echo json_encode(['success' => true, 'data' => $interventions]); break; case 'get_techniciens': $stmt = $db->query("SELECT * FROM techniciens WHERE actif = 1 ORDER BY nom"); echo json_encode(['success' => true, 'data' => $stmt->fetchAll()]); break; case 'demarrer': if ($method !== 'POST') throw new Exception('Méthode non autorisée', 405); $input = json_decode(file_get_contents('php://input'), true); $id = $input['id'] ?? 0; $stmt = $db->prepare("SELECT i.*, c.nom as client_nom, c.prenom as client_prenom, c.email as client_email, c.adresse as client_adresse, t.nom as technicien_nom FROM interventions i JOIN clients c ON i.client_id = c.id LEFT JOIN techniciens t ON i.technicien_id = t.id WHERE i.id = ?"); $stmt->execute([$id]); $inter = $stmt->fetch(); if (!$inter) throw new Exception('Intervention non trouvée', 404); if ($inter['statut'] === 'en_cours' || $inter['statut'] === 'terminé') { throw new Exception('Cette intervention est déjà en cours ou terminée', 400); } $db->prepare("UPDATE interventions SET statut = 'en_cours', progression = 10, date_debut = NOW(), email_debut_envoye = TRUE WHERE id = ?") ->execute([$id]); // Envoi email simulation $clientNom = $inter['client_prenom'] . ' ' . $inter['client_nom']; $sujet = "[Homeplus] Début de votre intervention"; $message = "Bonjour {$clientNom},\n\nNotre technicien a débuté l'intervention."; // mail($inter['client_email'], $sujet, $message); echo json_encode(['success' => true, 'message' => 'Intervention démarrée']); break; case 'terminer': if ($method !== 'POST') throw new Exception('Méthode non autorisée', 405); $input = json_decode(file_get_contents('php://input'), true); $id = $input['id'] ?? 0; $stmt = $db->prepare("SELECT i.*, c.nom as client_nom, c.prenom as client_prenom, c.email as client_email, c.adresse as client_adresse, t.nom as technicien_nom FROM interventions i JOIN clients c ON i.client_id = c.id LEFT JOIN techniciens t ON i.technicien_id = t.id WHERE i.id = ?"); $stmt->execute([$id]); $inter = $stmt->fetch(); if (!$inter) throw new Exception('Intervention non trouvée', 404); if ($inter['statut'] !== 'en_cours') { throw new Exception('Seules les interventions en cours peuvent être terminées', 400); } $db->prepare("UPDATE interventions SET statut = 'terminé', progression = 100, date_fin = NOW(), email_fin_envoye = TRUE WHERE id = ?") ->execute([$id]); // Envoi email simulation $clientNom = $inter['client_prenom'] . ' ' . $inter['client_nom']; $sujet = "[Homeplus] Fin de votre intervention"; $message = "Bonjour {$clientNom},\n\nNotre technicien a terminé l'intervention."; // mail($inter['client_email'], $sujet, $message); echo json_encode(['success' => true, 'message' => 'Intervention terminée']); break; default: throw new Exception('Action non reconnue', 404); } } catch (Exception $e) { http_response_code($e->getCode() ?: 500); echo json_encode(['success' => false, 'error' => $e->getMessage()]); } exit; } // Initialisation de la base de données au premier chargement if (!isset($_SESSION['db_initialized'])) { initDatabase(); $_SESSION['db_initialized'] = true; } ?> ?>